package top.wsuo.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import top.wsuo.factory.ServiceFactory;
import top.wsuo.model.User;

@WebServlet("/user/register.do")
public class UserRegisterAction extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        User user = new User();
        user.setUsername(username);
        user.setName(name);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        try {
            ServiceFactory.createUserService().createUser(user);
            System.out.println("用户注册成功");
            response.sendRedirect("login.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("用户注册失败");
            response.sendRedirect("registerFailed.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request, response);
    }

}
